using System.Collections.Generic;
using NodeCanvas.Framework;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI.Node;

namespace com.yoozoo.gta
{
    public class NpcEvadeGrenade : NavMeshMovement
    {
        public float EvadeDistance = 10; 
        public BBParameter<List<GameObject>> m_Grenades;
        private Vector3 GrenadePos;
        private Vector3 targetMovePos;
        protected override void OnExecute()
        {
            base.OnExecute();
            if (m_Grenades.value == null || m_Grenades.value.Count == 0)
            {
                EndAction(false);
            }
            else
            {
                // Multiple grenades can be specified. If multiple grenades are specified then evade from the center point of all of the grenades.
                var centerPoint = Vector3.zero;
           
                for (int i = 0; i < m_Grenades.value.Count; ++i) {
                    centerPoint += m_Grenades.value[i].transform.position;
                }
                centerPoint /= m_Grenades.value.Count;
                var direction = _transform.InverseTransformDirection(_transform.position - centerPoint);
                direction.y = 0;

                targetMovePos = direction;
                SetDestination(_transform.position + _transform.TransformDirection(targetMovePos.normalized) * EvadeDistance);
            }
        }

        protected override void OnUpdate()
        {
            base.OnUpdate();
            
            if (_entity.seeker.ReachedEndOfPath()) {
                EndAction(true);
            }
        }

        protected override void OnStop()
        {
            base.OnStop();
            m_Grenades.value.Clear();
        }
    }
}
